package io.renren.modules.secretary.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.renren.common.enums.AuditState;
import io.renren.common.utils.PageUtils;
import io.renren.common.utils.R;
import io.renren.modules.generator.entity.AuditEntity;
import io.renren.modules.generator.service.AuditService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Map;


/**
 * @author guijing
 * @email sunlightcs@gmail.com
 * @date 2022-02-05 22:24:19
 */
@RestController
@RequestMapping("secretary/audit")
public class SecretaryAuditController {
    @Autowired
    private AuditService auditService;


    /**
     * 列表
     */
    @RequestMapping("/list")
    public R list(@RequestParam Map<String, Object> params) {
        Page<Map<String, Object>> page = new Page<>( Long.valueOf(String.valueOf(params.get("page"))),  Long.valueOf(String.valueOf(params.get("limit"))));
        IPage<Map<String, Object>> mapIPage = auditService.selectSecretaryAuditList(page, params);
        PageUtils newPage = new PageUtils(mapIPage);

        return R.ok().put("page", newPage);

    }

    /**
     * 全部审核列表
     */
    @RequestMapping("/allList")
    public R allList(@RequestParam Map<String, Object> params) {
        Page<Map<String, Object>> page = new Page<>( Long.valueOf(String.valueOf(params.get("page"))),  Long.valueOf(String.valueOf(params.get("limit"))));
        IPage<Map<String, Object>> mapIPage = auditService.allStudentAuditList(page, params);
        PageUtils newPage = new PageUtils(mapIPage);

        return R.ok().put("page", newPage);

    }


    /**
     * 信息
     */
    @RequestMapping("/info/{auditId}")
    public R info(@PathVariable("auditId") Integer auditId) {
        AuditEntity audit = auditService.getById(auditId);

        return R.ok().put("audit", audit);
    }

    /**
     * 保存
     */
    @RequestMapping("/save")
    public R save(@RequestBody AuditEntity audit) {
        auditService.save(audit);

        return R.ok();
    }

    /**
     * 书记通过请假
     */
    @RequestMapping("/pass")
    @Transactional
    public R pass(@RequestBody Integer id) {
        AuditEntity audit = new AuditEntity();
        audit.setAuditId(id.longValue());
        //设置书记审核的时间
        audit.setAuditTime(new Date());
        audit.setAuditStatus(AuditState.ADOPT.getState());
        auditService.updateById(audit);
        return R.ok();
    }

    /**
     * 书记驳回请假
     */
    @RequestMapping("/reject")
    public R reject(@RequestBody Integer id) {
        AuditEntity auditEntity = new AuditEntity();
        auditEntity.setAuditId(id.longValue());
        //设置书记审核时间
        auditEntity.setAuditTime(new Date());
        //设置审核状态为驳回
        auditEntity.setAuditStatus(AuditState.REJECT.getState());
        auditService.updateById(auditEntity);

        return R.ok();
    }

}
